import { mapSolve } from 'api/config'
import { getSaleRegion, getSaleProvince, getSaleCity, getChannelRegion, getChannelProvince, getChannelCity, getShopChannelData } from 'api/getData'
/**
 * @desc: This is Note
 * 日期数据处理函数
 * @author: WJ
 * @date: 2018/10/13 16:12:00
 **/
export const durationSwitch = (flag, setMonthNum) => {
    if(flag) {
      let initState = [{ name: '近一年', val: '12'}]
      let yearSet = new Date().getFullYear()
      setMonthNum = setMonthNum || 3
      let tepYear = []
      for (let i = 0 ; i < setMonthNum; i++){
        tepYear.push({ name: `${yearSet - i}年`, val: `${yearSet - i}` })
      }
      return initState.concat(tepYear)
    }else {
      return [
        { name: '最近半年', val: '6' },
        { name: '最近3个月', val: '3' },
        { name: '最近1个月', val: '1' }
      ]
    }
}

export const updateDuration = ( setMonthNum = 3 ) => {
  return {
    selectMonth: durationSwitch(),
    selectYear: durationSwitch(true, setMonthNum)
  }
}

/**
 * @desc: This is Note
 * @author: WJ
 * @date: 2018/11/20 13:47:33
 **/
export const mineCreateYear = ( setYearNum = 9) => {
  let tempSet = []
  let setNowYear = new Date().getFullYear()
  for (let num = 0 ; num < setYearNum; num++){
    tempSet.push({  label: `${setNowYear - num}年`, value: `${setNowYear - num}` })
  }
  return tempSet
}

/**
 * @desc: This is Note
 * 数据递归处理函数
 * @author: WJ
 * @date: 2018/10/13 16:12:00
 **/
export const treeSolve = (arr = {}) => {
  let temSet = []
  const dataSolve = (item) => {
    temSet.push(item.id)
    if(item.children){
      item.children.map(v =>{ dataSolve(v) })
    }else{
      temSet.includes(item.id) ? "" : temSet.push(item.id)
    }
  }
  dataSolve(arr)
  return temSet
}

export const treeSolveUpdate = (arrSet = []) => {
  let temSet = []
  const dataSolve = (item) => {
    temSet.push({ id: item.id, label: item.label })
    if(item.children){
      item.children.map(v =>{ dataSolve(v) })
    }else{
      temSet.find(v => v.id == item.id) ? "" : temSet.push({ id: item.id, label: item.label })
    }
  }
  arrSet.map(v => { dataSolve(v) })
  return temSet
}

export const treeSolveDis = (arrSet = [], temSet = []) => {
  const dataSolve = (item) => {
    temSet.includes(item.id) ? item.disabled = true : item.disabled = false
    if(item.children){
      item.children.map(v =>{ dataSolve(v) })
    }
  }
  arrSet.map(v => { dataSolve(v) })
  return arrSet
}

/**
 * @desc: This is Note
 * 地图数据处理函数
 * @author: WJ
 * @date: 2018/10/13 16:12:43
 **/

export const loadMapState = (name = 'china') => {
  let setCode = name == 'china' ? name : mapSolve.find(v => v.name == name).code
  return  import(`../../static/mapSet/${setCode}`).then(mapState => {return { name: name, map: mapState }})
}

/**
 * @desc: This is Note
 * 随机函数
 * @author: WJ
 * @date: 2018/10/13 16:13:09
 **/
export const randomSolve = () => {
  return Math.round(Math.random()*1000)
}

/**
 * @desc: This is Note
 * 地区获取处理函数
 * @author: WJ
 * @date: 2018/11/08 10:58:40
 **/
export const districtSolve = (paramSet = { region: "全部", province: ""},fn) => {
  let { region, province } = paramSet
  Promise.all([getChannelRegion(),getChannelProvince({ region }),getChannelCity({ region, province })]).then(res => {
    if(res){
      fn({ region: [...res[0].data], province: [...res[1].data], city: [...res[2].data] })
    }else {
      fn({ region: [], province: [], city: [] })
    }
  }).catch(err => {
    fn({ region: [], province: [], city: [] })
  })
}

export const districtTotalSolve = (function () {
  let tempSet = null
  function districtUpdate(paramSet = {region: "全部", province: ""},fn) {
    let { region, province } = paramSet
    if(tempSet){
      let mineSetPost = region ? [getChannelProvince({region}),getChannelCity({region, province})] : [getChannelCity({region, province})]
      Promise.all(mineSetPost).then(res => {
        if(res){
          let [ provinceSet = {}, citySet = {} ] = res || []
          region
            ? Object.assign(tempSet,{ province: ['全部',...provinceSet.data], city: ['全部',...citySet.data] })
            : Object.assign(tempSet,{ city: ['全部',...provinceSet.data] })
          fn(tempSet)
        }else {
          fn({ region: tempSet.region || [], province: [], city: [] })
        }
      }).catch(err => {
        fn({ region: [], province: [], city: [] })
      })
    }else {
      let mineSetPost = [getChannelRegion(),getChannelProvince({region}),getChannelCity({region, province})]
      Promise.all(mineSetPost).then(res => {
        if(res){
          let [ region = {}, province = {}, city = {} ] = res
          tempSet = { region: ['全部',...region.data], province: ['全部',...province.data], city: ['全部',...city.data] }
          fn(tempSet)
        }else {
          fn({ region: [], province: [], city: [] })
        }
      }).catch(err => {
        fn({ region: [], province: [], city: [] })
      })
    }
  }
  return districtUpdate
})()

export const districtDeploySolve = (paramSet = {"region": "","province":""},fn) => {
  let { region, province } = paramSet || {}
  Promise.all([getSaleRegion(),getSaleProvince({region}),getSaleCity({region, province})]).then(res => {
    if(res){
      let [ regionSet = {}, provinceSet = {}, citySet = {} ] = res || []
      fn({ region: ['全部',...regionSet.data], province: ['全部',...provinceSet.data], city: ['全部',...citySet.data] })
    }else {
      fn({ region: [], province: [], city: [] })
    }
  }).catch(err => {
    fn({ region: [], province: [], city: [] })
  })
}

/**
 * @desc: This is Note
 * 图片正则判断函数
 * @author: WJ
 * @date: 2018/11/13 13:22:06
 **/
export const isTrueReg = (testStr = '') => {
  const pattStr = /image/g
  return pattStr.test(testStr) ? 0 : 1
}
export const isTrueVd = (testStr = '') => {
  const srcStr = /video/g
  return srcStr.test(testStr) ? 0 : 1
}

/**
* @desc: This is Note
* 文件下载
* @author: coulis
* @date: 2018/11/22 19:43:19
*/
export  const downLoadDoc = (res) => {
  window.location.href = 'http://dips.tinyvoice.net/file/download?fileName='+res+'.xlsx'
}

/**
 * @desc: This is Note
 * 日期处理函数
 * @author: WJ
 * @date: 2018/11/23 13:18:12
 **/
export const dateSolveSet = () => {
  const dateNowSet = new Date()
  let yearSet = dateNowSet.getFullYear()
  let monthSet = ((dateNowSet.getMonth() + 1).toString()).padStart(2, '0')
  let daySet = dateNowSet.getDate().toString().padStart(2, '0')
  return { yearSet, monthSet, daySet }
}

/**
 * @desc: This is Note
 * Render
 * @author: WJ
 * @date: 2018/11/29 09:56:31
 **/
export const renderSet = (function () {
  function renderMeSet(id, textA, textB){
    this.mainSet = document.getElementById(id)
    let { clientWidth, clientHeight } = this.mainSet
    this.tempSet = document.getElementById('canvas_2018') || document.createElement('canvas')
    this.tempSet.width = `${clientWidth}`
    this.tempSet.height = `${clientHeight}`
    this.cxtSet = this.tempSet.getContext("2d")
    this.options = { id, w: clientWidth, h: clientHeight, textA: textA || '对比产品参数', textB: textB || '对比产品' }
    this.initRender()
  }

  renderMeSet.prototype = {
    initRender(){
      let { w, h, textA, textB } = this.options
      this.cxtSet.beginPath()
      this.cxtSet.moveTo(0,0);
      this.cxtSet.lineTo(w,h);
      this.cxtSet.strokeStyle = '#E0E8EF';
      this.cxtSet.lineWidth = '2';
      this.cxtSet.stroke();

      this.cxtSet.beginPath()
      this.cxtSet.font="bold normal 15px Arial bold";
      this.cxtSet.fillStyle = '#333';
      this.cxtSet.fillText(textA,10,148);
      this.cxtSet.stroke();

      this.cxtSet.beginPath()
      this.cxtSet.font="bold normal 15px Arial";
      this.cxtSet.fillStyle = '#333';
      this.cxtSet.fillText(textB,64,30);
      this.cxtSet.stroke();
      if(this.tempSet.id == ''){
        this.tempSet.id = 'canvas_2018'
        this.mainSet.appendChild(this.tempSet)
      }
    }
  }
  return renderMeSet
})()
